package com.jiwei.system.dao;

import com.jiwei.system.pojo.Book;
import com.jiwei.system.pojo.Borrow;
import com.jiwei.system.pojo.Things;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface ItemMapper {

    /**
     * 此处返回的bid和tid有时候数据与数据库不对 待解决 暂不影响后面使用
     * @param account
     * @return
     */
    @Select("select id,tid,bid,amount,start_date as startDate,end_date as endDate,member_name as memberName,item_name as itemName,member_account as memberAccount,status from borrow where member_account = #{account} order by start_date desc")
    List<Borrow> getMemberBorrowItemListByAccount(String account);

    @Select("select * from things order by tid asc limit #{start},#{limit}")
    List<Things> getThingsList(@Param(value = "start") int start, @Param(value = "limit") int limit);

    @Select("select count(*) from things ")
    int queryThingNum();

    @Select("select * from book order by bid asc limit #{start},#{limit}")
    List<Book> getBookList(@Param(value = "start") int start, @Param(value = "limit") int limit);

    @Select("select count(*) from book ")
    int queryBookNum();

    void insertBorrowItem(Borrow borrow);

    @Update("update borrow set status=#{status},end_date = now() where item_name=#{itemName} and id=#{id} and start_date=#{startDate} and amount=#{amount};")
    Boolean updateBorrowItem(Borrow borrow);

    @Select("select current_amount from things where tid =#{tid}")
    int getThingCurrentCountByTid(String tid);

    @Update("update things set current_amount =#{currentAmount} where tid=#{tid};")
    void updateCurrentCountByTid(Map<String, Object> attribute);

    @Select("select status from borrow where id =#{id}")
    String getStatusById(String id);

    @Select("select current_amount from book where bid =#{bid}")
    int getBookCurrentCountByBid(String bid);

    @Update("update book set current_amount =#{currentAmount} where bid=#{bid};")
    void updateCurrentCountByBid(Map<String, Object> attribute);

    @Select("select * from borrow order by end_date desc limit #{start},#{limit}")
    List<Borrow> getBorrowHistoryList(@Param(value = "start") int start, @Param(value = "limit") int limit);

    @Select("select count(*) from borrow ")
    int queryBorrowNum();

    void addNewThing(Things things);

    void addNewBook(Book book);

    @Update("update things set current_amount =#{currentAmount},total_amount =#{totalAmount} where id=#{id};")
    void modifyThing(Things things);

    @Update("update book set current_amount =#{currentAmount},total_amount =#{totalAmount},publisher =#{publisher},author =#{author} where id=#{id};")
    void modifyBook(Book book);

}
